<template>
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in dataList" :key="res.id"
								@click="lookdetails(res.id)">
								<view class="top">
									<view class="left">
										<view class="store">{{ '订单编号' + res.order_no }}</view>
									</view>
									<view class="right" v-if="res.status == 0">{{ '待付款' }}</view>
									<view class="right" v-if="res.status == 1">{{ '配送中' }}</view>
									<view class="right" v-if="res.status == 2">{{ '待收货' }}</view>
									<view class="right" v-if="res.status == 3">{{ '已完成' }}</view>
									<view class="right" v-if="res.status == 4">{{ '退款中' }}</view>
									<view class="right" v-if="res.status == 5">{{ '已退款' }}</view>
									<view class="right" v-if="res.status == -1">{{ '已取消' }}</view>
									<view class="right" v-if="res.status == -2">{{ '拒绝退款' }}</view>
								</view>
								<view class="item" v-for="(item, index) in res.goods" :key="index">
									<view class="left">
										<image :src="item.goods_images_format[0]" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ item.goods_name }}</view>
										<view class="title u-line-3">{{ item.goods_type }}</view>
										<view class="delivery-time">￥{{ item.goods_price }}</view>
									</view>
									<view class="right">
										<view class="number">x{{ item.goods_number }}</view>
									</view>
								</view>
								<view class="total">
									共{{ totalNum(res.goods) }}件商品 合计:
									<text class="total-price">
										￥{{ res.pay_amount }}
									</text>
								</view>
								<view class="bottom">
									<view class="btn" v-if="res.status == 0" @click.stop="cancelOrder(res.id)">取消订单
									</view>
									<view class="btn affirm" v-if="res.status == 0"
										@click.stop.prevent="onOrderPayment(res.order_no,res.total_amount)">立即支付</view>
									<view class="btn affirm" v-if="res.status == 2" @click.stop="affirmOrder(res.id)">
										确认收货
									</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in dataList" :key="res.id"
								@click="lookdetails(res.id)">
								<view class="top">
									<view class="left">
										<view class="store">{{ '订单编号' + res.order_no }}</view>
									</view>
									<view class="right" v-if="res.status == 0">{{ '待付款' }}</view>
									<view class="right" v-if="res.status == 1">{{ '配送中' }}</view>
									<view class="right" v-if="res.status == 2">{{ '待收货' }}</view>
									<view class="right" v-if="res.status == 3">{{ '已完成' }}</view>
									<view class="right" v-if="res.status == 4">{{ '退款中' }}</view>
									<view class="right" v-if="res.status == 5">{{ '已退款' }}</view>
									<view class="right" v-if="res.status == -1">{{ '已取消' }}</view>
									<view class="right" v-if="res.status == -2">{{ '拒绝退款' }}</view>
								</view>
								<view class="item" v-for="(item, index) in res.goods" :key="index">
									<view class="left">
										<image :src="item.goods_images_format[0]" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ item.goods_name }}</view>
										<view class="title u-line-3">{{ item.goods_type }}</view>
										<view class="delivery-time">￥{{ item.goods_price }}</view>
									</view>
									<view class="right">
										<view class="number">x{{ item.goods_number }}</view>
									</view>
								</view>
								<view class="total">
									共{{ totalNum(res.goods) }}件商品 合计:
									<text class="total-price">
										￥{{ res.pay_amount }}
									</text>
								</view>
								<view class="bottom">
									<view class="btn" v-if="res.status == 0" @click.stop="cancelOrder(res.id)">取消订单
									</view>
									<view class="btn affirm" v-if="res.status == 0"
										@click.stop="onOrderPayment(res.order_no,res.total_amount)">立即支付</view>
									<view class="btn affirm" v-if="res.status == 2" @click.stop="affirmOrder(res.id)">
										确认收货
									</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[1]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in dataList" :key="res.id"
								@click="lookdetails(res.id)">
								<view class="top">
									<view class="left">
										<view class="store">{{ '订单编号' + res.order_no }}</view>
									</view>
									<view class="right" v-if="res.status == 0">{{ '待付款' }}</view>
									<view class="right" v-if="res.status == 1">{{ '配送中' }}</view>
									<view class="right" v-if="res.status == 2">{{ '待收货' }}</view>
									<view class="right" v-if="res.status == 3">{{ '已完成' }}</view>
									<view class="right" v-if="res.status == 4">{{ '退款中' }}</view>
									<view class="right" v-if="res.status == 5">{{ '已退款' }}</view>
									<view class="right" v-if="res.status == -1">{{ '已取消' }}</view>
									<view class="right" v-if="res.status == -2">{{ '拒绝退款' }}</view>
								</view>
								<view class="item" v-for="(item, index) in res.goods" :key="index">
									<view class="left">
										<image :src="item.goods_images_format[0]" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ item.goods_name }}</view>
										<view class="title u-line-3">{{ item.goods_type }}</view>
										<view class="delivery-time">￥{{ item.goods_price }}</view>
									</view>
									<view class="right">
										<view class="number">x{{ item.goods_number }}</view>
									</view>
								</view>
								<view class="total">
									共{{ totalNum(res.goods) }}件商品 合计:
									<text class="total-price">
										￥{{ res.pay_amount }}
									</text>
								</view>
								<view class="bottom">
									<view class="btn" v-if="res.status == 0" @click.stop="cancelOrder(res.id)">取消订单
									</view>
									<view class="btn affirm" v-if="res.status == 0"
										@click.stop.prevent="onOrderPayment(res.order_no,res.total_amount)">立即支付</view>
									<view class="btn affirm" v-if="res.status == 2" @click.stop="affirmOrder(res.id)">
										确认收货
									</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[2]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in dataList" :key="res.id"
								@click="lookdetails(res.id)">
								<view class="top">
									<view class="left">
										<view class="store">{{ '订单编号' + res.order_no }}</view>
									</view>
									<view class="right" v-if="res.status == 0">{{ '待付款' }}</view>
									<view class="right" v-if="res.status == 1">{{ '配送中' }}</view>
									<view class="right" v-if="res.status == 2">{{ '待收货' }}</view>
									<view class="right" v-if="res.status == 3">{{ '已完成' }}</view>
									<view class="right" v-if="res.status == 4">{{ '退款中' }}</view>
									<view class="right" v-if="res.status == 5">{{ '已退款' }}</view>
									<view class="right" v-if="res.status == -1">{{ '已取消' }}</view>
									<view class="right" v-if="res.status == -2">{{ '拒绝退款' }}</view>
								</view>
								<view class="item" v-for="(item, index) in res.goods" :key="index">
									<view class="left">
										<image :src="item.goods_images_format[0]" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ item.goods_name }}</view>
										<view class="title u-line-3">{{ item.goods_type }}</view>
										<view class="delivery-time">￥{{ item.goods_price }}</view>
									</view>
									<view class="right">
										<view class="number">x{{ item.goods_number }}</view>
									</view>
								</view>
								<view class="total">
									共{{ totalNum(res.goods) }}件商品 合计:
									<text class="total-price">
										￥{{ res.pay_amount }}
									</text>
								</view>
								<view class="bottom">
									<view class="btn" v-if="res.status == 0" @click.stop="cancelOrder(res.id)">取消订单
									</view>
									<view class="btn affirm" v-if="res.status == 0"
										@click.stop.prevent="onOrderPayment(res.order_no,res.total_amount)">立即支付</view>
									<view class="btn affirm" v-if="res.status == 2" @click.stop="affirmOrder(res.id)">
										确认收货
									</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[3]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in dataList" :key="res.id"
								@click="lookdetails(res.id)">
								<view class="top">
									<view class="left">
										<view class="store">{{ '订单编号' + res.order_no }}</view>
									</view>
									<view class="right" v-if="res.status == 0">{{ '待付款' }}</view>
									<view class="right" v-if="res.status == 1">{{ '配送中' }}</view>
									<view class="right" v-if="res.status == 2">{{ '待收货' }}</view>
									<view class="right" v-if="res.status == 3">{{ '已完成' }}</view>
									<view class="right" v-if="res.status == 4">{{ '退款中' }}</view>
									<view class="right" v-if="res.status == 5">{{ '已退款' }}</view>
									<view class="right" v-if="res.status == -1">{{ '已取消' }}</view>
									<view class="right" v-if="res.status == -2">{{ '拒绝退款' }}</view>
								</view>
								<view class="item" v-for="(item, index) in res.goods" :key="index">
									<view class="left">
										<image :src="item.goods_images_format[0]" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ item.goods_name }}</view>
										<view class="title u-line-3">{{ item.goods_type }}</view>
										<view class="delivery-time">￥{{ item.goods_price }}</view>
									</view>
									<view class="right">
										<view class="number">x{{ item.goods_number }}</view>
									</view>
								</view>
								<view class="total">
									共{{ totalNum(res.goods) }}件商品 合计:
									<text class="total-price">
										<!-- ￥{{ totalPrice(res.goods) }} -->
										￥{{ res.pay_amount }}
									</text>
								</view>
								<view class="bottom">
									<view class="btn" v-if="res.status == 0" @click.stop="cancelOrder(res.id)">取消订单
									</view>
									<view class="btn affirm" v-if="res.status == 0"
										@click.stop.prevent="onOrderPayment(res.order_no,res.total_amount)">立即支付</view>
									<view class="btn affirm" v-if="res.status == 2" @click.stop="affirmOrder(res.id)">
										确认收货
									</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[4]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
		<!-- 支付方式 -->
		<u-popup v-model="show" mode="bottom" :closeable="true" border-radius="28">
			<view class="popBox">
				<view class="total_price">
					<span class="icon">￥</span>{{payPrice}}
				</view>
				<view class="payItemView">
					<view class="payTypeTitme">请选择支付方式</view>
					<view class="payTypeItem flc" @click="onSelecteds(1)">
						<view class="left">
							<u-icon name="/static/weixin.png" size="50"></u-icon>
							<view class="payTypeTitle">微信支付</view>
						</view>
		
						<view class="right">
							<u-icon name="/static/selected.png" size="40" v-if="currents == 1"></u-icon>
							<u-icon name="/static/unselected2.png" size="40" v-else></u-icon>
						</view>
					</view>
					<view class="payTypeItem flc" @click="onSelecteds(2)">
						<view class="left">
							<u-icon name="/static/icons/dfk.png" size="50"></u-icon>
							<view class="payTypeTitle">余额支付</view>
						</view>
					
						<view class="right">
							<u-icon name="/static/selected.png" size="40" v-if="currents == 2"></u-icon>
							<u-icon name="/static/unselected2.png" size="40" v-else></u-icon>
						</view>
					</view>
				</view>
				<view class="pay" @click="Pay">
					立即支付
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getOrderlist,
		getWechatPay,
		cancelOrderId,
		confirmOrderId,
		getCashPay
	} from '@/api/user.js';
	import {
		toast
	} from '../../utils/common';

	export default {
		data() {
			return {
				orderList: [
					[],
					[],
					[],
					[]
				],
				dataList: [],
				list: [{
						name: '全部'
					},
					{
						name: '待付款'
					},
					{
						name: '配送中'
					},
					{
						name: '待收货'
					},
					{
						name: '已完成',
					}
				],
				current: 0,
				currents:"",
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				page: 1,
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore', 'loadmore'],
				flag: false,
				show:false,
				orderNo:"",
				payPrice:""
			};
		},
		onLoad(options) {
			console.log("初始", options);
			this.current = parseInt(options.index) + 1;
			this.swiperCurrent = parseInt(options.index) + 1;
			if (options.index == -1) {
				this.getOrderList(this.page, "null")
			} else {
				this.getOrderList(this.page, this.current - 1);
			}
		},
		onReachBottom() {
			// this.loadStatus[this.current] = 'loading';

		},
		computed: {
			// 价格小数
			priceDecimal() {
				return val => {
					if (val !== parseInt(val)) return val.slice(-2);
					else return '00';
				};
			},
			
			// 价格整数
			priceInt() {
				return val => {
					if (val !== parseInt(val)) return val.split('.')[0];
					else return val;
				};
			}
		},
		methods: {
			reachBottom() {
				console.log(this.flag);
				if (this.flag) {
					setTimeout(() => {
						this.getOrderList(this.page, this.current)
					}, 500)
				}
			},
			// 页面数据
			getOrderList(page, idx) {
				let that = this;
				if (idx == 'undefined' || (idx < 0)) {
					idx = null
				}
				getOrderlist({
					page,
					status: idx
				}).then(res => {
					console.log(res);
					if (res.total == 0) {
						that.loadStatus.splice(this.current, 1, "nomore");
						that.dataList = []
					} else {
						let arr = []
						arr = res.list.map(item => {
							return {
								...item,
								goods: item.goods.map(items => {
									return {
										...items,
										goods_images_format: items.goods_skus ? [items
											.goods_sku_cover_format
										] : items.goods_images_format,
										goods_price: items.goods_skus ? items.goods_sku_price :
											items.goods_price,
										goods_type:items.goods_skus?"类型:"+JSON.parse(items.goods_skus)[0].name:""
											
									};
								})
							};
						});
						arr.forEach(item => {
							this.dataList.push(item)
						})
					}
					if (res.page < res.last_page) {
						this.page++
						this.flag = true
					} else {
						this.flag = false
					}
				})
			},
			// 总价
			totalPrice(item) {
				let price = 0;
				item.map(val => {
					price += parseFloat(val.goods_price) * val.goods_number;
				});
				return price.toFixed(2);
			},
			// 总件数
			totalNum(item) {
				let num = 0;
				item.map(val => {
					num += val.goods_number;
				});
				return num;
			},
			// tab栏切换
			change(index) {
				this.page = 1
				this.dataList = []
				this.falg = true
				this.swiperCurrent = index;
				this.getOrderList(this.page, (index - 1).toString());
			},
			// transition({ detail: { dx } }) {
			// 	this.$refs.tabs.setDx(dx);
			// },
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			},
			// 取消订单
			cancelOrder(id) {
				let that = this;
				cancelOrderId({
					order_id: id
				}).then(res => {
					that.getOrderList(that.page, that.current - 1);
				})
			},
			// 确认收货
			affirmOrder(id) {
				let that = this;
				confirmOrderId({
					order_id: id
				}).then(res => {
					toast('确认收货');
					that.getOrderList(that.page, that.current - 1);
				})
			},
			lookdetails(id) {
				uni.navigateTo({
					url: '/page/order-details/order-details?id=' + id
				})
			},
			onSelecteds(i) {
				this.currents = i
			},
			// 支付
			onOrderPayment(no,price) {
				this.show = true
				this.payPrice = price
				this.orderNo = no
			},
			Pay(no){
				if(this.currents==1){
					let self = this;
					getWechatPay({
						order_no: this.orderNo
					}).then(res => {
						// self.onPayment("wxpay", res.data);
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.package,
							signType: 'MD5',
							paySign: res.data.paySign,
							success: function(res) {
								// success && success(res.data);
								toast('支付成功');
								setTimeout(function() {
									uni.navigateBack({
										delta: 1
									});
								}, 500);
							},
							fail: function(err) {
								uni.showToast({
									title: '支付失败',
									icon: 'none',
									duration: 2000
								});
								// fail && fail(err.data);
							},
							complete(ress) {
								// complete && complete(ress.data);
							}
						});
					})
				}else{
					let self = this;
					uni.showLoading({
						title: "支付中"
					})
					getCashPay({
						order_no: this.orderNo
					}).then(ress => {
						uni.hideLoading()
						if (ress.code == 200) {
							toast("支付成功")
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/home/home'
								})
							}, 500)
						} else {
							toast(ress.message)
						}
					})
				}
			}
		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	// 支付方式
	.popBox {
		width: 750rpx;
		box-sizing: border-box;
		padding: 30rpx 20rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 0rpx 0rpx;
		height: 720rpx;
		position: relative;
	
		.total_price {
			margin-top: 40rpx;
			text-align: center;
			font-size: 56rpx;
			font-family: Source Han Sans CN;
			font-weight: 500;
			color: #333333;
	
			.icon {
				font-size: 36rpx;
			}
		}
	
		.pay {
			position: absolute;
			bottom: 40rpx;
			left: 30rpx;
			width: 690rpx;
			height: 80rpx;
			background: #ff553d;
			border-radius: 40rpx;
			text-align: center;
			line-height: 80rpx;
			color: #fff;
		}
	
	}
	.payItemView .payTypeItem{
		display: flex;
		justify-content: space-between;
		margin: 20rpx 0;
		.left{
			display: flex;
			width: 180rpx;
			justify-content: space-between;
			align-items: center;
		}
	}
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: #fc562f;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #FA453C;
					font-size: 30rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			// justify-content: space-between;
			justify-content: flex-end;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
				margin-left: 10upx;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}

			.affirm {
				background-color: #fc562f;
				color: #fff;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
	.u-line-3{
		font-size: 22rpx;
		color: #666666;
		max-width: 420rpx;
		min-width: 420rpx;
	}
</style>